package com.notice.repository;

import com.notice.model.MessageModel;
import com.notice.model.PhotoModel;
import com.notice.model.FileModel;
import com.notice.dao.MessageDao;
import com.notice.entity.Messages;
import com.notice.entity.Photo;
import com.notice.entity.file;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Repository
public class MessageRepository {
    
    @Autowired
    private MessageDao messageDao;
    
    public Integer add(MessageModel message) {
        if (message == null) return null;
        Messages entity = convertToEntity(message);
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        entity.setMtime(simpleDateFormat.format(date));
        if (messageDao.add(entity) > 0) {
            return entity.getMid();
        }
        return null;
    }
    
    public List<MessageModel> find(MessageModel query) {
        Messages entity = convertToEntity(query);
        List<Messages> messages = messageDao.find(entity);
        return messages != null ? messages.stream()
                .map(this::convertToModel)
                .collect(Collectors.toList())
                : new ArrayList<>();
    }
    
    public Object getDetail(Integer mid) {
        if (mid == null) return null;
        
        Messages query = new Messages();
        query.setMid(mid);
        List<Messages> messages = messageDao.find(query);
        if (messages == null || messages.isEmpty()) {
            return null;
        }
        
        Map<String, Object> detail = new HashMap<>();
        detail.put("messages", convertToModel(messages.get(0)));
        
        PhotoModel photoQuery = new PhotoModel();
        photoQuery.setMid(mid);
        List<Photo> photos = messageDao.findPhoto(convertToPhotoEntity(photoQuery));
        detail.put("photo", photos != null ? photos.stream()
                .map(this::convertToPhotoModel)
                .collect(Collectors.toList())
                : new ArrayList<>());
        
        FileModel fileQuery = new FileModel();
        fileQuery.setMid(mid);
        List<file> files = messageDao.findFile(convertToFileEntity(fileQuery));
        for(file f:files){
            System.out.println("1文件的类型为"+f.getKind());
        }
        detail.put("file", files != null ? files.stream()
                .map(this::convertToFileModel)
                .collect(Collectors.toList())
                : new ArrayList<>());
        
        return detail;
    }
    
    public boolean delete(Integer mid) {
        if (mid == null) return false;
        return messageDao.delete(mid) > 0;
    }
    
    public boolean update(MessageModel message) {
        if (message == null || message.getMid() == null) return false;
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        message.setMtime(simpleDateFormat.format(date));
        return messageDao.update(convertToEntity(message)) > 0;
    }
    
    public boolean addPhoto(PhotoModel photo) {
        if (photo == null || photo.getMid() == null) return false;
        return messageDao.addPhoto(convertToPhotoEntity(photo)) > 0;
    }
    
    public boolean addFile(FileModel fileModel) {
        if (fileModel == null || fileModel.getMid() == null) return false;
        return messageDao.addFile(convertToFileEntity(fileModel)) > 0;
    }
    
    public boolean updateFile(FileModel fileModel) {
        if (fileModel == null || fileModel.getFid() == null) return false;
        return messageDao.updateFile(convertToFileEntity(fileModel)) > 0;
    }
    
    public boolean updatePhoto(PhotoModel photo) {
        if (photo == null || photo.getPid() == null) return false;
        return messageDao.updatePhoto(convertToPhotoEntity(photo)) > 0;
    }
    
    public boolean deleteFile(FileModel fileModel) {
        if (fileModel == null || (fileModel.getFid() == null && fileModel.getMid() == null)) {
            return false;
        }
        try {
            return messageDao.deleteFile(convertToFileEntity(fileModel)) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean deletePhoto(PhotoModel photo) {
        if (photo == null || (photo.getPid() == null && photo.getMid() == null)) {
            return false;
        }
        try {
            int i = messageDao.deletePhoto(convertToPhotoEntity(photo));
            System.out.println("delete "+i);
            return  i> 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public List<FileModel> findFiles(FileModel fileModel) {
        if (fileModel == null) return new ArrayList<>();
        List<file> files = messageDao.findFile(convertToFileEntity(fileModel));
        return files != null ? files.stream()
                .map(this::convertToFileModel)
                .collect(Collectors.toList())
                : new ArrayList<>();
    }
    
    public List<PhotoModel> findPhotos(PhotoModel photo) {
        if (photo == null) return new ArrayList<>();
        List<Photo> photos = messageDao.findPhoto(convertToPhotoEntity(photo));
        return photos != null ? photos.stream()
                .map(this::convertToPhotoModel)
                .collect(Collectors.toList())
                : new ArrayList<>();
    }
    
    // 转换方法
    private Messages convertToEntity(MessageModel model) {
        if (model == null) return null;
        
        Messages entity = new Messages();
        entity.setMid(model.getMid());
        entity.setContext(model.getContext() != null ? model.getContext() : "");
        entity.setMtype(Integer.valueOf(model.getMtype() == null ? 0 : model.getMtype()));
        entity.setMtime(model.getMtime() != null ? model.getMtime() : "");
        entity.setHead(model.getHead() != null ? model.getHead() : "");
        return entity;
    }
    
    private MessageModel convertToModel(Messages entity) {
        if (entity == null) return null;
        
        MessageModel model = new MessageModel();
        model.setMid(entity.getMid());
        model.setContext(entity.getContext() != null ? entity.getContext() : "");
        model.setMtype(entity.getMtype());
        model.setMtime(entity.getMtime() != null ? entity.getMtime() : "");
        model.setHead(entity.getHead() != null ? entity.getHead() : "");
        return model;
    }
    
    private Photo convertToPhotoEntity(PhotoModel model) {
        if (model == null) return null;
        
        Photo entity = new Photo();
        entity.setPhotodata(model.getPhotodata() != null ? model.getPhotodata() : "");
        entity.setMid(model.getMid());
        entity.setPid(model.getPid());
        return entity;
    }
    
    private PhotoModel convertToPhotoModel(Photo entity) {
        if (entity == null) return null;
        
        PhotoModel model = new PhotoModel();
        model.setPhotodata(entity.getPhotodata() != null ? entity.getPhotodata() : "");
        model.setMid(entity.getMid());
        model.setPid(entity.getPid());
        return model;
    }
    
    private file convertToFileEntity(FileModel model) {
        if (model == null) return null;
        
        file entity = new file();
        entity.setFiledata(model.getFiledata() != null ? model.getFiledata() : "");
        entity.setMid(model.getMid());
        entity.setFid(model.getFid());
        entity.setKind(model.getKind());
        return entity;
    }
    
    private FileModel convertToFileModel(file entity) {
        if (entity == null) return null;
        
        FileModel model = new FileModel();
        model.setFiledata(entity.getFiledata() != null ? entity.getFiledata() : "");
        model.setMid(entity.getMid());
        model.setFid(entity.getFid());
        model.setKind(entity.getKind());
        return model;
    }
} 